<template>
  <div class="wrap" ref="container"></div>
</template>

<script>
const echarts = require('echarts/lib/echarts')
require('echarts/lib/chart/bar')
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
export default {
  props: {
    chartData: Array
  },
  data() {
    return {
      chart: null,
      option: {
        title: {
          text: 'ECharts 入门示例'
        },
        tooltip: {},
        legend: {},
        xAxis: {
          type: 'category',
          axisLabel: {
            show: true,
            color: '#fff'
          }
        },
        yAxis: {},
        series: [
          {
            type: 'bar'
          },
          {
            type: 'bar'
          },
          {
            type: 'bar'
          },
          {
            type: 'bar'
          }
        ]
      }
    }
  },
  methods: {
    refresh() {
      this.chart.setOption({
        dataset: {
          source: this.chartData
        }
      })
    },
    init() {
      var chart = echarts.init(this.$refs.container)
      chart.setOption(this.option)
      this.chart = chart
    }
  },
  mounted() {
    this.init()
  },
  watch: {
    chartData() {
      this.refresh()
    }
  }
}
</script>

<style scoped>
.wrap {
  width: 100%;
  height: 100%;
}
</style>
